﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_GoodSalesEdit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //清除页面缓存
        Response.Expires = 0;
        Response.Cache.SetNoStore();
        Response.AppendHeader("Pragma", "no-cache"); 

        tbAmount.Attributes.Add("onfocusout", "return AmountChange()"); //数量发生变化时

        ReadAction();
        BindData();
    }

    #region 窗体设计器生成的代码

    protected override void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestLoginPage = "BalanceCenter/GoodSalesEdit.aspx";

        base.OnInit(e);
    }

    #endregion

    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (this.hID.Value != "")
        {
            if (tbAmount.Text.Trim() == "")
            {
                ShoveEL._Web.JavaScript.Alert(this.Page, "请输入购买数量");
                return;
            }
            else
            {
                if (ShoveEL._Convert.StrToDouble(tbAmount.Text.Trim(), -1) <= 0)
                {
                    ShoveEL._Web.JavaScript.Alert(this.Page, "请输入正确的购买数量");
                    return;
                }
            }

            if (ddlPayType.SelectedValue == "")
            {
                ShoveEL._Web.JavaScript.Alert(this.Page, "请选择支付方式");
                return;
            }

            long UserID = _User.ID;                                                         //会员ID
            long TypeID = ShoveEL._Convert.StrToLong(hID.Value,0);                          //类别ID
            int Amount = ShoveEL._Convert.StrToInt(tbAmount.Text.Trim(),0);                 //购买数量
            double Price = ShoveEL._Convert.StrToDouble(hGoodTypePrice.Value, 0);           //单价
            double Total = ShoveEL._Convert.StrToDouble((Amount * Price).ToString(), 0);    //总价
            int PayType = ShoveEL._Convert.StrToInt(ddlPayType.SelectedValue, 0);           //支付方式

            int ReturnValue = 0;
            string ReturnDescription = "";

            try
            {
                DAL.Procedures.P_GoodSales.Call(UserID, TypeID, Amount, Total, PayType, ref ReturnValue,ref ReturnDescription);

                if (ReturnValue < 0)
                {
                    ShoveEL._Web.JavaScript.Alert(this.Page, ReturnDescription);
                    return;
                }

                ShoveEL._Web.JavaScript.Alert(this.Page, "购买成功!");                     

                tbAmount.Text = "";
                tbSumPrice.Text = "";

                btnOK.Enabled = false;
            }
            catch { }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("GoodSalesManager.aspx");
    }

    private void ReadAction()
    {
        this.hID.Value = ShoveEL._Web.Utility.GetRequest("id");
    }  

    private void BindData()
    {
        if (this.hID.Value != "")
        {
            long ID = ShoveEL._Convert.StrToLong(hID.Value, -1);
            DataTable dt = null;

            try
            {
                dt = DAL.Tables.T_GoodType.Select("", "[ID]=" + ID, "");

                if (dt == null)
                {
                    PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);
                    return;
                }

                ImageTypePic.ImageUrl = "../UpFiles/" + dt.Rows[0]["TypePic"].ToString();
                tbGoodName.Text = dt.Rows[0]["TypeName"].ToString();
                tbRemark.Text = dt.Rows[0]["Remark"].ToString();

                //if (_User.UserType == 1)
                //{
                    tbPrice.Text = dt.Rows[0]["Price"].ToString();
                    hGoodTypePrice.Value = dt.Rows[0]["Price"].ToString();  //供JS中使用
                //}
                //else if (_User.UserType == 3)
                //{
                //    tbPrice.Text = dt.Rows[0]["Price2"].ToString();
                //    hGoodTypePrice.Value = dt.Rows[0]["Price2"].ToString();  
                //}
                //else if (_User.UserType == 5)
                //{
                //    tbPrice.Text = dt.Rows[0]["Price3"].ToString();
                //    hGoodTypePrice.Value = dt.Rows[0]["Price3"].ToString();
                //}
                //else if (_User.UserType == 10)
                //{
                //    tbPrice.Text = dt.Rows[0]["Price4"].ToString();
                //    hGoodTypePrice.Value = dt.Rows[0]["Price4"].ToString();
                //}

                //电子货币支付与返还货币支付
                bool IsVirtualSales = ShoveEL._Convert.StrToBool(dt.Rows[0]["IsVirtualSales"].ToString(), false);
                //bool IsBvSales = ShoveEL._Convert.StrToBool(dt.Rows[0]["IsBvSales"].ToString(), false);

                if (IsVirtualSales)
                {                    
                    ddlPayType.Items.Add(new ListItem("电子货币", "2"));
                }
                //if (IsBvSales)
                //{
                //    ddlPayType.Items.Add(new ListItem("返还货币", "3"));
                //}
            }
            catch { }

        }
    }

}
